package io.binghe.concurrent.lab06;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

/**
 * 创建一个单线程的调度线程池，支持定时任务和周期任务，但始终只有一个线程，任务按顺序执行
 * @author CurleyG
 * @date 2025/3/25 23:55
 */
public class SingleThreadScheduledExecutorExample {
    public static void main(String[] args) {
        ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor();

        Runnable task = () -> System.out.println(Thread.currentThread().getName() + " 执行任务，时间：" + System.currentTimeMillis());

        // 延迟 2 秒执行
        scheduler.schedule(task, 2, TimeUnit.SECONDS);

        scheduler.shutdown();
    }
}
